package org.dromara.nursing.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.nursing.domain.bo.MSContractInfoBo;
import org.dromara.nursing.domain.vo.MSContractInfoVo;
import org.dromara.nursing.domain.MSContractInfo;
import org.dromara.nursing.mapper.MSContractInfoMapper;
import org.dromara.nursing.service.IMSContractInfoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 合同信息Service业务层处理
 *
 * @author Xue
 * @date 2025-04-23
 */
@RequiredArgsConstructor
@Service
public class MSContractInfoServiceImpl implements IMSContractInfoService {

    private final MSContractInfoMapper baseMapper;

    /**
     * 查询合同信息
     *
     * @param contractId 主键
     * @return 合同信息
     */
    @Override
    public MSContractInfoVo queryById(Long contractId){
        return baseMapper.selectVoById(contractId);
    }

    /**
     * 分页查询合同信息列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 合同信息分页列表
     */
    @Override
    public TableDataInfo<MSContractInfoVo> queryPageList(MSContractInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<MSContractInfo> lqw = buildQueryWrapper(bo);
        Page<MSContractInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的合同信息列表
     *
     * @param bo 查询条件
     * @return 合同信息列表
     */
    @Override
    public List<MSContractInfoVo> queryList(MSContractInfoBo bo) {
        LambdaQueryWrapper<MSContractInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<MSContractInfo> buildQueryWrapper(MSContractInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MSContractInfo> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(MSContractInfo::getContractId);
        lqw.like(StringUtils.isNotBlank(bo.getElderName()), MSContractInfo::getElderName, bo.getElderName());
        lqw.eq(StringUtils.isNotBlank(bo.getRoomNumber()), MSContractInfo::getRoomNumber, bo.getRoomNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getBedNumber()), MSContractInfo::getBedNumber, bo.getBedNumber());
        lqw.eq(bo.getStartDate() != null, MSContractInfo::getStartDate, bo.getStartDate());
        lqw.eq(bo.getEndDate() != null, MSContractInfo::getEndDate, bo.getEndDate());
        lqw.eq(StringUtils.isNotBlank(bo.getContractType()), MSContractInfo::getContractType, bo.getContractType());
        lqw.like(StringUtils.isNotBlank(bo.getContactName()), MSContractInfo::getContactName, bo.getContactName());
        lqw.eq(StringUtils.isNotBlank(bo.getReviewStatus()), MSContractInfo::getReviewStatus, bo.getReviewStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getServiceLevel()), MSContractInfo::getServiceLevel, bo.getServiceLevel());
        return lqw;
    }

    /**
     * 新增合同信息
     *
     * @param bo 合同信息
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(MSContractInfoBo bo) {
        MSContractInfo add = MapstructUtils.convert(bo, MSContractInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setContractId(add.getContractId());
        }
        return flag;
    }

    /**
     * 修改合同信息
     *
     * @param bo 合同信息
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(MSContractInfoBo bo) {
        MSContractInfo update = MapstructUtils.convert(bo, MSContractInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(MSContractInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除合同信息信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
